package com.bestcem.xm.user.dao.impl;

import com.bestcem.xm.user.dao.RolePermissionDao;
import com.bestcem.xm.user.dao.mapper.RolePermissionMapper;
import com.bestcem.xm.user.entity.mysql.RolePermission;
import com.bestcem.xm.user.entity.pojo.RolePermissionDO;
import com.bestcem.xm.user.util.business.DataBaseUtil;
import com.bestcem.xm.user.util.convert.RolePermissionConvert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色权限 Dao
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/17 15:01
 */
@Repository
public class RolePermissionDaoImpl implements RolePermissionDao {

    @Resource
    private RolePermissionConvert rolePermissionConvert;

    @Resource
    private RolePermissionMapper rolePermissionMapper;


    @Override
    public String insertSelective(RolePermissionDO rolePermissionDo) {
        RolePermission rolePermission = rolePermissionConvert.do2Entity(rolePermissionDo);
        DataBaseUtil.setCreateAndUpdateTime(rolePermission);
        rolePermission.setRolePermissionId(StringUtils.isNotBlank(rolePermission.getRolePermissionId())
                ? rolePermission.getRolePermissionId() : DataBaseUtil.generateId());
        rolePermissionMapper.insertSelective(rolePermission);
        return rolePermission.getRolePermissionId();
    }

    @Override
    public Integer updateByPrimaryKeySelective(RolePermissionDO rolePermissionDo) {
        RolePermission rolePermission = rolePermissionConvert.do2Entity(rolePermissionDo);
        DataBaseUtil.setUpdateTime(rolePermission);
        return rolePermissionMapper.updateByPrimaryKeySelective(rolePermission);
    }


    @Override
    public Integer deleteRolePermissionByRoleId(String roleId) {
        return rolePermissionMapper.deleteRolePermissionByRoleId(roleId);
    }

    @Override
    public RolePermissionDO selectByOrgIdAndRoleId(String orgId, String roleId) {
        return rolePermissionConvert.entity2DO(rolePermissionMapper.selectByOrgIdAndRoleId(orgId, roleId));
    }

    @Override
    public List<RolePermissionDO> listByRoleIds(List<String> roleIds, String orgId) {
        return rolePermissionConvert.mysqls2Dos(rolePermissionMapper.listByRoleIds(roleIds, orgId));
    }

    @Override
    public Integer updateByIds(List<String> roleIds, RolePermissionDO rolePermissionDO) {
        // 设置参数
        RolePermission rolePermission = rolePermissionConvert.do2Entity(rolePermissionDO);
        DataBaseUtil.setUpdateTime(rolePermission);
        // 更新
        return rolePermissionMapper.updateByIds(roleIds, rolePermission);
    }
}
